﻿
#region Usings
using System;
using System.Net.Mail;
using System.Threading;
#endregion

namespace LongNet.Web.Email
{
    /// <summary>
    /// Utility for sending an email
    /// </summary>
    public class EmailSender : Message
    {

        /*
         * smtp.sina.com
         * pop.sina.com
         * smtp.qq.com  Success
         * smtp.gail.com
         * smtp.aol.com
         * smtp.139.com
         * smtp.163.com
         * smtp.126.com
         * smtp.yeah.net
         * gmail smtp.gmail.com pop.gmail.com 
            foxmail.com.cn pop.foxmail.com.cn smtp.foxmail.com.cn 
            mail.bodachina.com mail.bodachina.com mail.bodachina.com 
            21cn.com pop.21cn.com smtp.21cn.com 
            sina.com pop3.sina.com.cn smtp.sina.com.cn 
            vip.sina.com pop3.vip.sina.com.cn smtp.vip.sina.com.cn 
            163.net pop.163.net smtp.163.net 
            tom.com pop.tom.com smtp.tom.com 
            163.com pop.163.com smtp.163.com 
            126.com pop3.126.com smtp.126.com 
            fm365.com mail.fm365.com mail.fm365.com 
            263.net 263.net smtp.263.net 
            x263.net pop.x263.net smtp.x263.net 
            263.net.cn 263.net.cn 263.net.cn 
            elong.com pop3.elong.com smtp.elong.com 
            chinaren.com pop.chinaren.com 　 
            km169.net email.km169.net email.km169.net 
            cmmail.com pop3.cmmail.com smtp.cmmail.com 
            china.com pop.china.com smtp.china.com 
            cww.com pop.china.com smtp.china.com 
            eazier.com pop.easier.com smtp.eazier.com 
            sohu.com pop3.sohu.com smtp.sohu.com 
            fd21.com pop.fd21.com smtp.fd21.com 
            online.sh.cn pop.citiz.net smtp.citiz.net 
            yeah.net pop.yeah.net smtp.yeah.net 
            netease.com pop.netease.com smtp.netease.com 
            etang.com pop.free.etang.com smtp.free.etang.com 
            myrice.com pop.myrice.com smtp.myrice.com 
            wx88.net mail.wx88.net mail.wx88.net 
            email.com.cn www.email.com.cn www.email.com.cn 
            cn99.com pop.cn99.com 　 
            tonghua.com.cn pop3.tonghua.com.cn smtp.tonghua.com.cn 
         * 
         */
        #region Constructors

        /// <summary>
        /// Default Constructor
        /// </summary>
        public EmailSender()
        {
            Attachment_ = null;
            Priority_ = MailPriority.Normal;
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Sends an email
        /// </summary>
        /// <param name="Message">The body of the message</param>
        public void SendMail(string Message)
        {
            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                char[] Splitter = { ',' };
                string[] AddressCollection = To.Split(Splitter);
                for (int x = 0; x < AddressCollection.Length; ++x)
                {
                    message.To.Add(AddressCollection[x]);
                }
                message.Subject = Subject;
                message.From = new System.Net.Mail.MailAddress((From));
                if (string.IsNullOrEmpty(Message))
                    Message = Content;
                //message.Body = Message;
                message.Priority = Priority_;
                message.SubjectEncoding = System.Text.Encoding.GetEncoding("utf-8");
                message.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                message.IsBodyHtml = true;
                if (Attachment_ != null)
                {
                    message.Attachments.Add(Attachment_);
                }

                //AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content", new System.Net.Mime.ContentType("text/plain"));
                //AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Content, new System.Net.Mime.ContentType("text/html"));
                string fileName = LongNet.Util.Utils.GetFullPath(LongNet.Util.HttpUtil.GetAppSettings("LongNet.LogFolder", false, false))+"\\temp.html";
                LongNet.Util.FileObj.WriteFile(fileName, Content);
                //AlternateView plainView = AlternateView.CreateAlternateViewFromString(Content, message.BodyEncoding, "text/plain");
                //AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Content, message.BodyEncoding, "text/html");
                AlternateView htmlView = new AlternateView(fileName, new System.Net.Mime.ContentType("text/html"));
                //LinkedResource logo = new LinkedResource("d:\\beautiful.jpg");
                //logo.ContentId = "companylogo";
                //htmlView.LinkedResources.Add(logo);
                //message.AlternateViews.Add(plainView);
                message.AlternateViews.Add(htmlView);

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Server,Port);
                if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
                {
                    smtp.Credentials = new System.Net.NetworkCredential(UserName,Password);
                }               
                smtp.Send(message);                
                message.Dispose();
                LongNet.Util.FileObj.FileDel(fileName);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        /// <summary>
        /// Sends a piece of mail asynchronous
        /// </summary>
        /// <param name="Message">Message to be sent</param>
        public void SendMailAsync(string Message)
        {
            ThreadPool.QueueUserWorkItem(delegate { SendMail(Message); });
        }

        #endregion

        #region Properties

        /*
        /// <summary>
        /// Whom the message is to
        /// </summary>
        public string To
        {
            get
            {
                return to;
            }
            set
            {
                to = value;
            }
        }

        /// <summary>
        /// The subject of the email
        /// </summary>
        public string Subject
        {
            get
            {
                return subject;
            }
            set
            {
                subject = value;
            }
        }


        /// <summary>
        /// The Message of the email
        /// </summary>
        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }
        /// <summary>
        /// Whom the message is from
        /// </summary>
        public string From
        {
            get
            {
                return from;
            }
            set
            {
                from = value;
            }
        }
*/
        /// <summary>
        /// Any attachments that are included with this
        /// message.
        /// </summary>
        public Attachment Attachment
        {
            get
            {
                return Attachment_;
            }
            set
            {
                Attachment_ = value;
            }
        }

        /// <summary>
        /// The priority of this message
        /// </summary>
        public MailPriority Priority
        {
            get
            {
                return Priority_;
            }
            set
            {
                Priority_ = value;
            }
        }

        /*
        /// <summary>
        /// Server Location
        /// </summary>
        public string Server
        {
            get { return Server_; }
            set { Server_ = value; }
        }

        /// <summary>
        /// User Name for the server
        /// </summary>
        public string UserName
        {
            get { return UserName_; }
            set { UserName_ = value; }
        }

        /// <summary>
        /// Password for the server
        /// </summary>
        public string Password
        {
            get { return Password_; }
            set { Password_ = value; }
        }

        /// <summary>
        /// Port to send the information on
        /// </summary>
        public int Port
        {
            get { return Port_; }
            set { Port_ = value; }
        }
        */
        #endregion

        #region Variables

        private MailPriority Priority_;
        private Attachment Attachment_;
        /*
        private string from;
        private string subject;
        private string content;
        private string to;
        private string Server_;
        private string UserName_;
        private string Password_;
        private int Port_;
        */
        #endregion
    }
}